package tech.yangzhijie.basic.knowledge.other;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class CallableTest {

    public static void main(String[] args) {
        CallableDemo callableDemo = new CallableDemo();
        FutureTask<Integer> futureTask = new FutureTask<>(callableDemo);

        for (int i = 0; i < 5; i++) {
            new Thread(futureTask).start();
        }

        try {
            Integer integer = futureTask.get();
            System.out.println(integer);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

class CallableDemo implements Callable<Integer>{

    private int num = 0;

    @Override
    public Integer call() throws Exception {
        for (int i=0;i<=100;i++){
            num+=i;
        }
        return num;
    }
}
